﻿Imports System.Data.SqlClient
Module KetNoi
    Public ob_cnn As New SqlConnection
    Dim m_Connection As New SqlConnection
    Dim m_DataAdapter As New SqlDataAdapter
    Dim m_Command As New SqlCommand

    Public Function f_KetNoi(ByVal S As String, ByVal D As String, ByVal U As String, ByVal P As String) As Boolean
        Dim str_Cnn As String = "database=" & D & " ; server=" & S & " ; UID=" & U & " ; PWD=" & P
        Try
            ob_cnn.ConnectionString = str_Cnn
            ob_cnn.Open()
            '     MessageBox.Show("Ket noi thanh cong", "thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return True
        Catch ex As Exception
            MessageBox.Show("Kết nối không thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return False
        End Try
    End Function
    Public Sub ExecuteNonQuery(ByVal sql As String)
        ob_cnn.Open()
        m_Command = New SqlCommand(sql, ob_cnn)
        m_Command.CommandType = CommandType.Text
        m_Command.ExecuteNonQuery()

        ob_cnn.Close()
    End Sub
    Public Function ExecuteQuery(ByVal sql As String) As DataTable
        Dim dt As New DataTable()
        ob_cnn.Open()
        m_DataAdapter = New SqlDataAdapter(sql, ob_cnn)
        m_DataAdapter.Fill(dt)
        If (dt.Rows.Count = 0) Then
            'Return Nothing
        End If
        ob_cnn.Close()
        Return dt
    End Function
    'Public Function ExecuteReader(ByVal sql As String) As DataTable
    '    Dim dt As New DataTable()
    '    ob_cnn.Open()
    '    m_Command = New SqlCommand(sql, ob_cnn)
    '    m_DataReader = m_Command.ExecuteReader()
    '    While (m_DataReader.HasRows)
    '        dt.Rows.Add(m_DataReader.)
    '    End While
    '    dt = m_DataReader
    '    If (dt.Rows.Count = 0) Then
    '        'Return Nothing
    '    End If

    '    ob_cnn.Close()
    '    Return dt
    'End Function
End Module
